package city.spring.modules.health.controller;

import city.spring.modules.health.entity.DiseaseEntity;
import city.spring.modules.health.service.DiseaseService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：疾病信息表
 *
 * @author 侯坤林
 * @date 2020-07-07 10:20:49
 */
@RestController
@RequestMapping("/disease")
public class DiseaseController extends ApiController {
    private final DiseaseService diseaseService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<DiseaseEntity, ?>> entityOrderFields;

    public DiseaseController(DiseaseService diseaseService) {
        this.diseaseService = diseaseService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(DiseaseEntity::getId);
        entityOrderFields.add(DiseaseEntity::getTitle);
        entityOrderFields.add(DiseaseEntity::getRemarks);
        entityOrderFields.add(DiseaseEntity::getVersion);
        entityOrderFields.add(DiseaseEntity::getGmtCreated);
        entityOrderFields.add(DiseaseEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>疾病信息表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, DiseaseEntity entity) {
        LambdaQueryChainWrapper<DiseaseEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>疾病信息表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, DiseaseEntity entity) {
        LambdaQueryChainWrapper<DiseaseEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<DiseaseEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>疾病信息表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(diseaseService.getById(id));
    }

    /**
     * 添加一个 <strong>疾病信息表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody DiseaseEntity entity) {
        diseaseService.saveDisease(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>疾病信息表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody DiseaseEntity entity) {
        entity.setId(id);
        diseaseService.updateDisease(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>疾病信息表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        diseaseService.deleteDisease(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>疾病信息表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        diseaseService.deleteDisease(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<DiseaseEntity> buildLambdaQuery(DiseaseEntity entity) {
        LambdaQueryChainWrapper<DiseaseEntity> lambdaQuery = diseaseService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), DiseaseEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTitle()), DiseaseEntity::getTitle, entity.getTitle());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRemarks()), DiseaseEntity::getRemarks, entity.getRemarks());
        lambdaQuery.eq(entity.getVersion() != null, DiseaseEntity::getVersion, entity.getVersion());
        lambdaQuery.eq(entity.getGmtCreated() != null, DiseaseEntity::getGmtCreated, entity.getGmtCreated());
        lambdaQuery.eq(entity.getGmtModified() != null, DiseaseEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<DiseaseEntity> buildQuery(DiseaseEntity entity) {
        QueryWrapper<DiseaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getTitle()), "title", entity.getTitle());
        wrapper.like(StringUtils.isNotBlank(entity.getRemarks()), "remarks", entity.getRemarks());
        wrapper.eq(entity.getVersion() != null, "version", entity.getVersion());
        wrapper.eq(entity.getGmtCreated() != null, "gmt_created", entity.getGmtCreated());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
